#include "isr.h"
#include "idt.h"
#include "i386.h"

#define IDT_ENTRY(numero) \
	idt[numero].offset_0_15 = (unsigned short) ((unsigned int)(&_isr ## numero) & (unsigned int) 0xFFFF); \
	idt[numero].segsel = (unsigned short) 0x0030; \
	idt[numero].attr = (unsigned short) 0x8E00; \
	idt[numero].offset_16_31 = (unsigned short) ((unsigned int)(&_isr ## numero) >> 16 & (unsigned int) 0xFFFF);

#define TASK_GATE_ENTRY(numero) \
	idt[numero].offset_0_15 = (unsigned short) 0x0; \
	idt[numero].segsel = (unsigned short) 0x0008; \
	idt[numero].attr = (unsigned short) 0x8500; \
	idt[numero].offset_16_31 = (unsigned short) 0x0;


void inicializar_idt() {
	IDT_ENTRY(0);
	IDT_ENTRY(1); //RESRVED mnemonic(#DB)
	IDT_ENTRY(2); // interrupt
	IDT_ENTRY(3); //  breakpoint mnemonic(#BP)
	IDT_ENTRY(4); // OVERFLOW MNEMONIC(#OF)
	IDT_ENTRY(5); // BOUND RANGE EXCEEDED MNEMONIC(#BR)
	IDT_ENTRY(6); // INVALID OPCODE MNEMONIC(#UD)
	IDT_ENTRY(7); // DEVICE NOT AVAILABLE MNEMONIC(#NM)
	IDT_ENTRY(8); // DOUBLE FAULT MNEMONIC(#DF)
	IDT_ENTRY(9); // COPROCESSOR SEGMENT OVERRUN MNEMONIC()
	IDT_ENTRY(10); // INVALID TSS MNEMONIC(#TS)
	IDT_ENTRY(11); // SEGMENT NOT PRESENT MNEMONIC(#NP)
	IDT_ENTRY(12); // STACK-SEGMENT FAULT MNEMONIC(#SS)
	IDT_ENTRY(13); // GENERAL PROTECTION MNEMONIC(#GP)
	IDT_ENTRY(14); // PAGE FAULT MNEMONIC(#PF)
	IDT_ENTRY(15); // INTEL RESERVED MNEMONIC()
	IDT_ENTRY(16); // FPU FLOATING POINT ERROR MNEMONIC(#MF)
	IDT_ENTRY(17); // ALIGNMENT CHECK MNEMONIC(#AC)
	IDT_ENTRY(18); // MACHINE CHECK MNEMONIC(#MC)
	IDT_ENTRY(19); // SIMD FLOATING POINT EXCEPTION MNEMONIC(#XF)
    
    IDT_ENTRY(32); // CLOCK
    IDT_ENTRY(33); // TECLADO
    
    //~ IDT_ENTRY(66); // FORK
    TASK_GATE_ENTRY(66); // FORK
    
    IDT_ENTRY(77); // 
    IDT_ENTRY(88); //
    IDT_ENTRY(99); //
}

idt_entry idt[255] = {};

idt_descriptor IDT_DESC = {sizeof(idt)-1, (unsigned int)&idt};


